<!DOCTYPE html>
<html>

<head>
    <title>斐波那契数列计算器</title>
</head>

<body>

    <div style='height:5px;'></div>
    <div>
        <script>
            var i = 0;
            var fibs = [];
            function fib(a) {
                var x, y, z;
                switch (a) {
                    case 0: case 1: x = a; break;
                    default:
                        if (!fibs[a - 1]) {
                            fibs[a - 1] = fib(a - 1);
                        }
                        if (!fibs[a - 2]) {
                            fibs[a - 2] = fib(a - 2);
                        }

                        y = fibs[a - 1];
                        z = fibs[a - 2];
                        x = y + z;
                }
                return x;
            }
            function doFib() {
                var n = parseInt(document.getElementById("n").value);
                if ((n) % 1 !== 0) {
                    document.getElementById("t").innerHTML = "输入超出范围或不合法";
                    return;
                }
                else if (isNaN(n) || n < 1) {
                    document.getElementById("t").innerHTML = "输入超出范围或不合法";
                    return;
                }
                else if (isNaN(n) || n > 1000) {
                    document.getElementById("t").innerHTML = "输入超出范围或不合法";
                    return;
                }
                else {
                    document.getElementById("t").innerHTML = "第" + document.getElementById("n").value + "个斐波那契数是:";
                }
                var a;
                var r = document.getElementById("r");
                r.value = "";
                for (var i = 0; i < n + 1; i++) {
                    a = fib(i);
                    r.value = a;
                }
            }</script>
        <TABLE cellSpacing=0 cellPadding=2 width=525 border=0 align="center">
            <TBODY>
                <TR>
                    <TD style="PADDING-BOTTOM: 30px; PADDING-TOP: 30px" vAlign=top align=middle>
                        <P>输入数值: <INPUT id=n></P>
                        <P id=t>请输入斐波那契数的序号</P>
                        <p><textarea id=r rows="1" name="S1" cols="29"></textarea></p>
                        <P><INPUT onclick=doFib(); type=button value=计算></P>
                    </TD>
                </TR>
            </TBODY>
        </TABLE>
    </div>
</body>

</html>